package com.ae.shield.client.gui;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.ae.shield.ModLib;
import com.ae.shield.client.gui.util.GuiHelper;
import com.ae.shield.common.gui.container.NanoRobotProgrammerContainer;
import com.ae.shield.common.items.shieldFittings.makers.NanoRobot;
import com.ae.shield.common.net.ModNetwork;
import com.ae.shield.common.net.toSever.ScreenButtonPressed;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.fml.client.gui.widget.ExtendedButton;

import javax.annotation.Nonnull;
import java.util.ArrayList;

public class NanoRobotProgrammerScreen extends ContainerScreen<NanoRobotProgrammerContainer> {
    private static final ResourceLocation GUI = new ResourceLocation(ModLib.MOD_ID, "textures/gui/nano_robot_programmer.png");
    private final GuiHelper guiHelper;

    public NanoRobotProgrammerScreen(NanoRobotProgrammerContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
        super(screenContainer, inv, titleIn);
        guiLeft = 0;
        guiTop = 0;
        xSize = 176;
        ySize = 208;
        guiHelper = new GuiHelper(this, xSize, ySize);
    }

    @Override
    protected void init() {
        int left = (width - xSize) / 2;
        int up = (height - ySize) / 2;
        Button button = new ExtendedButton(left + 78, up + 58, 20, 15, new StringTextComponent(" "), (b) -> {
            if (isNanoShieldIn()) {
                ModNetwork.CHANNEL.sendToServer(new ScreenButtonPressed(container.getPos(), 0));
            }
        });
        addButton(button);
        super.init();
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(@Nonnull MatrixStack matrixStack, float partialTicks, int x, int y) {
        //背景
        RenderSystem.color4f(1f, 1f, 1f, 1f);
        getMinecraft().textureManager.bindTexture(GUI);
        blit(matrixStack, guiLeft, guiTop, 0, 0, 176, 208);
        //能量
        int i = (int) Math.floor(container.getEnergyScaled() * 58);
        blit(matrixStack, guiLeft + 108, guiTop + 97, 176, 0, i, 3);
        //纳米工具特殊背景
        if (container.isNanoToolIn()) {
            blit(matrixStack, guiLeft + 25, guiTop + 42, 176, 7, 16, 16);
            blit(matrixStack, guiLeft + 58, guiTop + 14, 176, 23, 16, 16);
            blit(matrixStack, guiLeft + 100, guiTop + 14, 176, 39, 16, 16);
            blit(matrixStack, guiLeft + 133, guiTop + 42, 176, 55, 16, 16);
        }
        //纳米护盾效果图标
        drawEffectIcon();

    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        renderHoveredTooltip(matrixStack, mouseX, mouseY);
        drawEffectTip(mouseX, mouseY);
        drawEnergy(matrixStack, mouseX, mouseY);
    }

    @Override
    protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
        font.func_243248_b(matrixStack, playerInventory.getDisplayName(), (float) playerInventoryTitleX, (float) playerInventoryTitleY + 33, 4210752);
    }

    private void drawEffectIcon() {
        ItemStack itemStack = container.getInv().getStackInSlot(0);

        if (itemStack.isEmpty() || !(itemStack.getItem() instanceof NanoRobot)) {
            return;
        }

        NanoRobot nano = (NanoRobot) itemStack.getItem();
        for (int i = 0; i < 4; i++) {
            EffectInstance effectInstance = nano.readEffect(itemStack, i);

            if (effectInstance == null || !container.getInv().getStackInSlot(i + 1).isEmpty()) {
                continue;
            }

            switch (i) {
                case 0:
                    guiHelper.drawEffectIcon(effectInstance, 25, 42);
                    break;
                case 1:
                    guiHelper.drawEffectIcon(effectInstance, 58, 14);
                    break;
                case 2:
                    guiHelper.drawEffectIcon(effectInstance, 100, 14);
                    break;
                case 3:
                    guiHelper.drawEffectIcon(effectInstance, 133, 42);
                    break;
            }
        }
    }

    public void drawEffectTip(int mouseX, int mouseY) {
        ItemStack itemStack = container.getInv().getStackInSlot(0);
        Item item = itemStack.getItem();

        if (itemStack.isEmpty() || !(item instanceof NanoRobot)) {
            return;
        }

        NanoRobot nano = (NanoRobot) item;
        for (int i = 0; i < 4; i++) {
            EffectInstance instance = nano.readEffect(itemStack, i);

            if (instance == null || !container.getInv().getStackInSlot(i + 1).isEmpty()) {
                continue;
            }

            switch (i) {
                case 0:
                    guiHelper.drawEffectTip(instance, mouseX, mouseY, 25, 42, 18, 18);
                    break;
                case 1:
                    guiHelper.drawEffectTip(instance, mouseX, mouseY, 58, 14, 18, 18);
                    break;
                case 2:
                    guiHelper.drawEffectTip(instance, mouseX, mouseY, 100, 14, 18, 18);
                    break;
                case 3:
                    guiHelper.drawEffectTip(instance, mouseX, mouseY, 133, 42, 18, 18);
                    break;
            }
        }
    }

    private boolean isNanoShieldIn() {
        return container.getInv().getStackInSlot(0).getItem() instanceof NanoRobot;
    }

    private void drawEnergy(@Nonnull MatrixStack matrixStack, int mouseX, int mouseY) {
        if (107 + guiLeft < mouseX && mouseX < 166 + guiLeft && 96 + guiTop < mouseY && mouseY < 100 + guiTop) {
            ArrayList<ITextComponent> list = new ArrayList<>();
            list.add(new TranslationTextComponent("screen.ae.shield.assembly.energy").append(new StringTextComponent(container.getEnergy() + " RF")));
            func_243308_b(matrixStack, list, mouseX, mouseY);
        }
    }
}
